home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / ACTLIB.ZIP / TOOLS.ZIP / SETUP.C < prev    next >
C/C++ Source or Header  |  1993-09-23  |  9KB  |  304 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4. #include "tools.h"
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11.  
  12.  
  13.  
  14. /***
  15.  *
  16.  *  Function   :   getparam
  17.  *
  18.  *  Topics     :   Read one paramater/topic in setup file.
  19.  *
  20.  *  Parameters :    in    FILE *file           input file
  21.  *                  in    const char *keyword  keyword to match
  22.  *                  out   char *value          value associated to keyword
  23.  *                  in    topicPos             topic position in file
  24.  *
  25.  *  Values     :    topicPos   -1        whole file
  26.  *                              0        from beginning of file to first topic
  27.  *                              other    line after topic
  28.  *
  29.  *  Return code:     0     OK
  30.  *                  -1     keyword not found
  31.  *                  -2     file problem
  32.  *
  33.  *  Precond    :   Input file must be opened with read access.
  34.  *
  35.  */
  36.  
  37. static int near getparam( FILE *file, const char *keyword, char *value, long topicPos )
  38. {
  39.  char buffer[256];
  40.  int length, loop;
  41.  long inipos;
  42.  
  43.  length = strlen( keyword );
  44.  
  45.  inipos = ftell( file );
  46.  for ( loop = 0; loop < 2; loop++ )
  47.     {
  48.      while ( fgets(buffer, 255, file) )
  49.         {
  50.          if ( *buffer == '#' ) continue;
  51.  
  52.          strreduce( buffer );
  53.  
  54.          if ( *keyword == '[' )
  55.             {
  56.              char *ptr = strstr( buffer, keyword );
  57.              if ( ptr && *(ptr + length) == ']'  )
  58.                 return 0;
  59.              continue;
  60.             }
  61.  
  62.          if ( ! strncmp(buffer, keyword, length) )
  63.             switch( buffer[length] )
  64.             {
  65.              case '=':
  66.                  strcpy( value, buffer + length + 1);
  67.              return 0;
  68.  
  69.              case '\0':
  70.                  *value = '\0';
  71.              return 0;
  72.             }
  73.  
  74.          if ( *buffer == '[' && (topicPos >= 0) ) break;  /* new topic */
  75.  
  76.          if ( loop && (ftell(file) >= inipos) ) return -1;
  77.  
  78.     }  /*  while ( fgets )  */
  79.  
  80.      if ( fseek(file, max(topicPos, 0), SEEK_SET) ) return -2;    
  81.  
  82.     }  /* for ( loop )  */
  83.  
  84.  return -1;
  85. }
  86.  
  87.  
  88.  
  89. /***
  90.  *
  91.  *  Function   :   getsetup
  92.  *
  93.  *  Topics     :   Read paramaters in setup file (like Windows init files).
  94.  *
  95.  *  Decisions  :   Expected file format is a list of lines of the form
  96.  *                                         
  97.  *                   [topic1] [topic2] ... [topicN]  (optional)
  98.  *                    or
  99.  *             <keyword>=<value>
  100.  *
  101.  *                 - If a [topic] is given, keyword is searched only between
  102.  *                   that [topic] line and the next one.
  103.  *
  104.  *                 - several [topic] names may be given on the same line
  105.  *                   to be aliases for the same section.
  106.  *
  107.  *                 - All letters are treated (and returned) as uppercases.
  108.  *                 - Blanks and tabs are skipped.
  109.  *
  110.  *                 - <value> can be enclosed between double quotes
  111.  *             to preserve lowercases and blanks.
  112.  *
  113.  *            - 2 consecutive double quotes allow to keep
  114.  *                   a double quote in <value>.
  115.  *
  116.  *                 - A number sign (#) in first column forces the line
  117.  *                   to be ignored.
  118.  *
  119.  *                 - The input formats in parameter 'format' are C standard
  120.  *                   ones ( %d, %s, %f,...).
  121.  *                   Values to be stored are C standard
  122.  *                   ones ( &myInt, myString, &myFloat,...).
  123.  *
  124.  *                 - Special format added: "%b" will translate
  125.  *                   True/False, Yes/No, On/Off, 1/0 into integer 1/0
  126.  *
  127.  *                 - Special format added: "%S" will result into copying
  128.  *                   all the input line (with spaces in it if any).
  129.  *                   It will not be processed by scanf-like processing.
  130.  *
  131.  *  Parameters :    in    FILE *file        input file
  132.  *                  in    char *format      "[topic1] keyword1=format keyword2=format [topic2] ..."
  133.  *                  out   pointer list      values to be stored
  134.  *
  135.  *
  136.  *  Optimization:   Keywords (or topics) are searched from the current
  137.  *                  position of the file; if not found, search is
  138.  *                  redone from the [topic] line (or the begin of
  139.  *                  the file if no [topic] asked).
  140.  *                  That means that it is very more efficient
  141.  *                  (less disk access) to sort parameters
  142.  *                  in 'format' in the same order as encountered
  143.  *                  in the file.
  144.  *
  145.  *  Return code:    number of found parameters
  146.  *                  -1     syntax error
  147.  *                  -2     file error
  148.  *                  -3     not enough memory
  149.  *
  150.  *  Precond    :   Input file must be opened with read access.
  151.  *
  152.  */
  153.  
  154. int getsetup( FILE *file, const char *format, ... )
  155. {
  156.  va_list list;
  157.  int status, count = 0;
  158.  char par_value[255], *topic = NULL, *fmt, *keyword, *ptr;
  159.  void *param;
  160.  long topicPos = -1;
  161.  
  162.  va_start( list, format );
  163.  fmt = strdup( format );
  164.  if ( ! fmt ) return -3;
  165.  
  166.  for ( keyword = strtok(fmt, "="); keyword; keyword = strtok(NULL, "=") )
  167.     {
  168.      param = va_arg( list, void* );
  169.  
  170.      strreduce( keyword );
  171.      if ( *keyword == '[' )    /* search for a [topic] line */
  172.         {
  173.          char *ptr = strchr( keyword, ']' );
  174.          if ( ptr )
  175.             {
  176.              topic = keyword;
  177.              *ptr++ = '\0';
  178.              keyword = ptr;
  179.  
  180.              /* search topic entry */
  181.              status = getparam( file, topic, NULL, -1 );
  182.              switch( status )
  183.              {
  184.               case  0:
  185.                  topicPos = ftell( file );
  186.                  if ( topicPos < 0 )
  187.                     {
  188.                      free( fmt );
  189.                      return -2;
  190.                     }
  191.                  break;
  192.  
  193.               case -1:
  194.                  if ( *keyword != '*' )  /* first section if topic not found */
  195.                     topic = NULL;
  196.                  else
  197.                     {
  198.                      topicPos = 0;
  199.                      if ( fseek(file, 0, SEEK_SET) )
  200.                         {
  201.                          free( fmt );
  202.                          return -2;
  203.                         }
  204.                     }
  205.                  break;
  206.  
  207.               default:
  208.                  free( fmt );
  209.                  return status;
  210.  
  211.              }  /*  switch( status )  */
  212.  
  213.              if ( *keyword == '*' )  /* first section if topic not found */
  214.                 keyword++;
  215.  
  216.             }  /*  if ( ptr )  */
  217.  
  218.         } /*  if ( *keyword == '[' ) */
  219.  
  220.      if ( topicPos >= 0 && ! topic ) continue;
  221.  
  222.      status = getparam( file, keyword, par_value, topicPos );
  223.      format = strtok( NULL, " \t\r\n" );
  224.      switch( status )
  225.      {
  226.       case  0: break;
  227.  
  228.       case -1: continue;
  229.  
  230.       default: free( fmt );
  231.                return status;
  232.      }
  233.  
  234.      count++;
  235.  
  236.      /* handle boolean (%b) format */
  237.      ptr = strstr( format, "%b" );
  238.      if ( ptr )
  239.         {
  240.          if ( ! *par_value ||
  241.               strisequal(par_value, "TRUE") ||
  242.               strisequal(par_value, "YES") ||
  243.               strisequal(par_value, "ON") ||
  244.               !strncmp(par_value, "ENABLE", 6) ||
  245.               strisequal(par_value, "1")
  246.             ) *(int*)param = 1;
  247.          else *(int*)param = 0;
  248.  
  249.          continue;
  250.         }  /*  if ( ptr )  */
  251.  
  252.  
  253.      /* Handle %S to get spaces inside strings */
  254.      ptr = strstr( format, "%S" );
  255.      if ( ptr )
  256.         {
  257.          strcpy( ((char*)param), par_value );
  258.          continue;
  259.         }
  260.  
  261.      if ( sscanf(par_value, format, param) != 1 )
  262.         {
  263.          free( fmt );
  264.          return -1;
  265.         }
  266.  
  267.     }   /*  for (;;)  */
  268.  
  269.  va_end( list );
  270.  
  271.  free( fmt );
  272.  return count;
  273. }
  274.  
  275.  
  276. #ifdef MAIN
  277.  
  278. void main( void )
  279. {
  280.  FILE *inFile;
  281.  char buffer[10][50];
  282.  int flag;
  283.  
  284.  inFile = fopen( "setup.dat", "rt" );
  285.  if ( ! inFile ) return;
  286.  
  287.  memset( buffer, '\0', sizeof(buffer) );
  288.  fseek( inFile, 200, SEEK_SET );
  289.  getsetup( inFile